package com.century.sjt.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.entity.MyOrderInfo;

import java.util.List;

/**
 * @author:周玲波
 * @Description: 商家、分店订单详情适配器
 * @date : 2016/9/5 16:15
 */
public class MerchantOrderDetailsAdapter extends BaseAdapter{
    Context mContext;
    List<MyOrderInfo> mList;
    LayoutInflater inflater;
    private Handler mHandler;

    public MerchantOrderDetailsAdapter(Context mContext, List<MyOrderInfo> mList, Handler mHandler) {
        this.mContext = mContext;
        this.mList = mList;
        this.mHandler = mHandler;
        inflater = LayoutInflater.from(mContext);
    }



    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.sjt_myorder_details_lv_item, null);
            holder.layoutDelete = (LinearLayout) convertView.findViewById(R.id.layout_delete_order);
            holder.iv_goods_icon = (ImageView) convertView.findViewById(R.id.goods_icon);
            holder.tv_unit_price = (TextView) convertView.findViewById(R.id.tv_unit_price);
            holder.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
            holder.icon_select2 = (TextView) convertView.findViewById(R.id.icon_select2);
            holder.tv_order_num = (TextView) convertView.findViewById(R.id.tv_order_num);
            holder.tv_goods_unit_price = (TextView) convertView.findViewById(R.id.tv_goods_unit_price);
            holder.tv_goods_description = (TextView) convertView.findViewById(R.id.tv_goods_description);
            Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), "iconfont.ttf");
            holder.icon_select2.setTypeface(iconfont);
            holder.countNumber = (EditText) convertView.findViewById(R.id.count_number);
            holder.countAdd = (LinearLayout) convertView.findViewById(R.id.count_add);
            holder.countReduce = (LinearLayout) convertView.findViewById(R.id.count_reduce);
            holder.ll_order = (LinearLayout) convertView.findViewById(R.id.ll_order);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_unit_price.setText(mList.get(position).getUnitPrice());
        holder.tv_total.setText(mList.get(position).getTotal());
        String count = mList.get(position).getGoodsQuantity();

        mList.get(position).setCountNumber(Integer.valueOf(count));
        if (null == count || "".equals(count)) {
            holder.countNumber.setText("1");
        } else {
            holder.countNumber.setText(count + "");
        }

        //只有当未支付的时候才可以修改订单
//        if (mInfo.getStatus().equals("1")) {
//            holder.tv_order_num.setVisibility(View.GONE);
//            holder.ll_order.setVisibility(View.VISIBLE);
//            holder.tv_goods_unit_price.setVisibility(View.GONE);
//            holder.tv_unit_price.setVisibility(View.VISIBLE);
//            holder.icon_select2.setVisibility(View.VISIBLE);
//        } else {
            holder.ll_order.setVisibility(View.GONE);
            holder.tv_order_num.setVisibility(View.VISIBLE);
            holder.tv_order_num.setText(mList.get(position).getGoodsQuantity());
            holder.icon_select2.setVisibility(View.GONE);
            holder.tv_goods_unit_price.setVisibility(View.VISIBLE);
            holder.tv_goods_unit_price.setText(mList.get(position).getUnitPrice());
            holder.tv_unit_price.setVisibility(View.GONE);
      //  }
        Glide.with(mContext).load(mList.get(position).getImage()).into(holder.iv_goods_icon);
        return convertView;
    }

    class ViewHolder {
        ImageView iv_goods_icon;
        TextView tv_unit_price;
        TextView tv_total;
        TextView icon_select2;
        TextView tv_order_num;
        TextView tv_goods_unit_price;
        TextView tv_goods_description;
        EditText countNumber;
        LinearLayout countAdd;
        LinearLayout countReduce;
        LinearLayout layoutDelete;
        LinearLayout ll_order;
    }
}
